package com.dongdongshop.controller;

import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongsop.data.AjaxResult;
import com.dongdongsop.data.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("typeTemplate")
public class TypeTemplateController {

    @DubboReference
    private TypeTemplateService typeTemplateService;

    @DubboReference
    private BrandService brandService;

    @DubboReference
    private SpecificationService specificationService;

    //跳转模块管理首页
    @RequestMapping("toTypeTemplate")
    public String toTypeTemplate(){
        return "admin/type_template";
    }

    //查询模块管理
    @RequestMapping("typeTemplateInfo")
    @ResponseBody
    public PageResult<TbTypeTemplate> typeTemplateInfo(@RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                                                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        PageResult<TbTypeTemplate> typeTemplateList = typeTemplateService.typeTemplateInfo(pageNumber,pageSize);
        return typeTemplateList;
    }

    //跳转模块添加页面，查询品牌，规格表
    @RequestMapping("toTypeTemplateAdd")
    public String toTypeTemplateAdd(Model model){
        List<TbBrand> brandList = brandService.listPage();
        List<TbSpecification> specificationList = specificationService.specificationInfo();
        model.addAttribute("brandList",brandList);
        model.addAttribute("specList",specificationList);
        return "typeTemplate/add";
    }

    //模块添加
    @RequestMapping("typeTemplateAdd")
    @ResponseBody
    public AjaxResult typeTemplateAdd(TbTypeTemplate typeTemplate){
        try {
            int i = typeTemplateService.typeTemplateAdd(typeTemplate);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //跳转模块添加页面,查询品牌，规格表并回显
    @RequestMapping("getTypeTemplateByBrandAllBySpecAll")
    public String getTypeTemplateByBrandAllBySpecAll(@RequestParam("id") Long id,Model model){
        TbTypeTemplate typeTemplate = typeTemplateService.getTypeTemplateByBrandAllBySpecAll(id);
        model.addAttribute("typeTemplate",typeTemplate);
        List<TbBrand> brandList = brandService.listPage();
        List<TbSpecification> specificationList = specificationService.specificationInfo();
        model.addAttribute("brandList",brandList);
        model.addAttribute("specList",specificationList);
        return "typeTemplate/update";
    }

    //模块修改
    @RequestMapping("updateTypeTemplate")
    @ResponseBody
    public AjaxResult updateTypeTemplate(TbTypeTemplate typeTemplate){
        try {
            int i = typeTemplateService.updateTypeTemplate(typeTemplate);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //根据ID删除模块数据
    @RequestMapping("deleteId")
    @ResponseBody
    public AjaxResult deleteId(Long id){
        try {
            int i = typeTemplateService.deleteId(id);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
